package logic

import (
	"context"
	"errors"
	"video/mysql/model"

	"video/videorpc/internal/svc"
	"video/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type UserUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpdateLogic {
	return &UserUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 个人资料修改
func (l *UserUpdateLogic) UserUpdate(in *videorpc.UserUpdateRequest) (*videorpc.UserUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var user model.User
	model.DB.Debug().Where("account = ?", in.Account).Find(&user)
	if user.ID == 0 {
		return nil, errors.New("该用户不存在,无法修改")
	}

	user = model.User{
		Model:     gorm.Model{ID: uint(in.UserId)},
		Account:   in.Account,
		Password:  in.Password,
		NickName:  in.NickName,
		HeadImage: in.HeadImage,
	}
	model.DB.Debug().Updates(&user)

	return &videorpc.UserUpdateResponse{
		UserId: int64(user.ID),
	}, nil
}
